//
//  ViewController.m
//  照片
//
//  Created by MB__Lee on 2019/1/17.
//  Copyright © 2019年 MB__Lee. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self composeImg];
    
}

- (void)composeImg {
    
    float a = 0.3;
    
    UIImage *img = [UIImage imageNamed:@"z.jpg"];
    CGImageRef imgRef = img.CGImage;
    CGFloat w = CGImageGetWidth(imgRef) *a;
    CGFloat h = CGImageGetHeight(imgRef) *a;
    
    //以1.png的图大小为底图
    UIImage *img1 = [UIImage imageNamed:@"zz.jpg"];
    CGImageRef imgRef1 = img1.CGImage;
    CGFloat w1 = CGImageGetWidth(imgRef1)*a;
    CGFloat h1 = CGImageGetHeight(imgRef1) *a;
    
    //以1.png的图大小为画布创建上下文
    UIGraphicsBeginImageContext(CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height));
    [img1 drawInRect:CGRectMake(100, 150, w1, h1)];//先把1.png 画到上下文中
    [img drawInRect:CGRectMake(100,h1 + 250, w, h)];//再把小图放在上下文中
    UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();//从当前上下文中获得最终图片
    UIGraphicsEndImageContext();//关闭上下文
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [path stringByAppendingPathComponent:@"01.png"];
    [UIImagePNGRepresentation(resultImg) writeToFile:filePath atomically:YES];//保存图片到沙盒
    
    NSLog(@"%@",filePath);
    
    CGImageRelease(imgRef);
    CGImageRelease(imgRef1);
    
    UIImageView *imgv = [[UIImageView alloc] initWithImage:resultImg];
    [self.view addSubview:imgv];
    self.view.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.9];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
